/*********************************************************************************
 *FileName:  nsuc16xx_flash.c
 *Version:  V1.0
 *Date: 2023-04-19
 ****************************************Includes***********************************/
#include "nsuc16xx_flash.h"

/***************************************Variables***********************************/

void Flash_DummyDelay(uint32_t cnt)
{
	volatile uint32_t i;
	for (i = 0; i < cnt; i++)
	{
		__NOP;
	}
}

void Flash_Unlock(void)
{
	/* unlock sysctrl register */
	SYSCTRL->LKKEYR = SYSCTRL_UNLOCK_KEY;

	/* unlock FLASH */
	FLASH_CTRL->OKR = FLASH_UNLOCK_KEY2;
	FLASH_CTRL->OKR = FLASH_UNLOCK_KEY3;
	FLASH_CTRL->OKR = FLASH_UNLOCK_KEY0;
	FLASH_CTRL->OKR = FLASH_UNLOCK_KEY1;
}

uint32_t Flash_WritePage(uint32_t page_address, uint32_t *data)
{
	uint32_t ret;
	__disable_irq();
	ret = RAM_Flash_WritePage(page_address, data);
	Flash_DummyDelay(10);
	__enable_irq();

	return ret;
}

uint32_t Flash_EraseSector(uint32_t page_address)
{
	uint32_t ret;

	__disable_irq();
	ret = RAM_Flash_EraseSector(page_address);
	Flash_DummyDelay(10);
	__enable_irq();
	return ret;
}

/***************************************Functions***********************************/
